package com.ea.eadp.nexus.test.pageDefinition.newconsole;

import javax.annotation.Nullable;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

import com.ea.eadp.nexus.test.common.TestUtil;

public class WelcomePage extends ConsoleBasePage {
    public final static String acceptCheckBox = "label-readAccept";

    public WelcomePage(WebDriver driver) {
        super(driver, WelcomePage.class, acceptCheckBox);
    }

    public void navigate(String uri) {
        driver.switchTo().window(operationWindowHandle);
        log.info(uri);
        driver.navigate().to(uri);
    }

    public void acceptTOS() {
        log.info("Select Accept TOS CheckBox");
        selectCheckBox(acceptCheckBox);
        sendControllerKey('A');
    }

    public void pressBack() {
        log.info("Press Back Button");
        sendControllerKey('B');
    }

    public String getTitle() {
        return getElementStringByXPath("//div[@id='panel-legal']//span[@class='title']");
    }

    public String getDescription() {
        return getElementStringByXPath("//div[@id='panel-legal']//p[@class='description']");
    }

    public String getTosDescription() {
        return getElementStringByXPath("//label[@id='label-readAccept']");
    }

    public String getTosTitle() {
        sendControllerKey('X');
        String title = getElementStringByXPath("//div[@id='panel-tos-text']//span[@class='title']");
        sendControllerKey('B');
        return title;
    }

    public String getPrivacyTitle() {
        sendControllerKey('Y');
        String title = getElementStringByXPath("//div[@id='panel-privacy-text']//span[@class='title']");
        sendControllerKey('B');
        return title;
    }

    public String getButtonA() {
        String title = getElementStringByXPath("//div[@id='aBtn']");
        return title;
    }

    public String getButtonB() {
        String title = getElementStringByXPath("//div[@id='bBtn']");
        return title;
    }

    public String getButtonX() {
        String title = getElementStringByXPath("//div[@id='xBtn']");
        return title;
    }

    public String getButtonY() {
        String title = getElementStringByXPath("//div[@id='yBtn']");
        return title;
    }

    public void validateRedirect() throws Exception {
        WebDriverWait wait = new WebDriverWait(driver, 30);
        wait.until(new ExpectedCondition<Boolean>() {
            @Nullable
            @Override
            public Boolean apply(@Nullable WebDriver webDriver) {
                return TestUtil.getCurrentUrl(driver).contains("?code=");
            }
        });
    }
}
